<div navbar ng-init="currentItem='database'"></div>
<div ng-show="!loaded"><div loading></div></div>

<div id="main" ng-show="loaded" style="display:none">
	<div class="module-header">
		<h3>管理用户 {{username}}</h3>
	</div>
	
	<div class="well" style="width:350px" ng-show="!$mysql.password_validated">
		<form onsubmit="return false" method="get" action="/">
			<div class="control-group" style="margin-bottom:10px;">
				<label class="control-label" for="password">
					请输入数据库管理员 root 的密码后继续操作：
				</label>
				<input class="input" ng-model="$mysql.password" type="password" id="password" ng-disabled="processing" autofocus><br>
				<button class="btn btn-primary" ng-click="validate_password()" ng-disabled="processing">登录 MySQL</button>
				<a href="#/service/mysql?s=password" class="btn">忘记密码？</a>
			</div>
		</form>
	</div>

	<div class="tabbable" ng-init="load()" ng-show="$mysql.password_validated">
		<ul class="nav nav-tabs">
			<li ng-class="'active' | iftrue:activeTabName=='privs'"><a href="#privs" ng-click="sec('privs')" data-toggle="tab">用户权限</a></li>
			<li ng-class="'active' | iftrue:activeTabName=='operation'"><a href="#operation" ng-click="sec('operation')" data-toggle="tab">用户操作</a></li>
		</ul>
		<div class="tab-content">
			<div class="tab-pane" ng-class="'active' | iftrue:activeTabName=='privs'" id="privs">
				<div class="well" style="width:350px" ng-show="privsloading">
					<img src="images/loading.gif" style="margin-right:5px"> 正在加载权限列表，请稍候...
				</div>

				<div class="row-fluid">
					<div class="span8">
						<div class="widget widget-table" ng-show="!privsloading">
							<div class="widget-header">
								<i class="icon-user"></i><h3>按数据库指定的权限</h3>
							</div>
							<div class="widget-content">
								<table class="table table-striped table-hover table-bordered">
									<thead>
										<tr>
											<th style="width:20px" class="td-right">#</th>
											<th>数据库</th>
											<th>权限</th>
											<th>授权权限</th>
											<th style="width:90px">操作</th>
										</tr>
									</thead>
									<tbody>
										<tr ng-show="privs.bydb.length==0">
											<td colspan="5">该用户暂未设置数据库权限</td>
										</tr>
										<tr ng-repeat="dbpriv in privs.bydb">
											<td class="td-right">{{$index+1}}</td>
											<td>{{dbpriv.Db}}</td>
											<td ng-bind-html-unsafe="dbpriv | mysql.privs:'db'"></td>
											<td ng-bind-html-unsafe="dbpriv.Grant_priv | mysql.grant"></td>
											<td>
												<div class="btn-group">
													<button class="btn btn-small" ng-click="editprivs(dbpriv)">
														<i class="icon-edit"></i> 编辑权限
													</button>
												</div>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
						</div>
						<div class="widget" ng-show="!privsloading">
							<div class="widget-header">
								<i class="icon-edit"></i><h3>设置数据库权限</h3>
							</div>
							<div class="widget-content">
								<label>在下列数据库中设置用户权限：</label>
								<div class="input-append">
									<select ng-disabled="processing" ng-model="privs_dbname" ng-options="db.name as db.name for db in dbs"></select>
									<button class="btn" ng-disabled="processing" ng-click="editprivs()">设置权限</button>
								</div>
							</div>
						</div>
					</div>

					<div class="span4">
						<div class="widget widget-table" ng-show="!privsloading">
							<div class="widget-header">
								<i class="icon-user"></i><h3>全局权限</h3>
							</div>
							<div class="widget-content">
								<table class="table table-striped table-hover table-bordered">
									<thead>
										<tr>
											<th>权限</th>
											<th>授权权限</th>
											<th style="width:90px">操作</th>
										</tr>
									</thead>
									<tbody>
										<tr>
											<td ng-bind-html-unsafe="privs.global | mysql.privs:'global'"></td>
											<td ng-bind-html-unsafe="privs.global.Grant_priv | mysql.grant"></td>
											<td>
												<div class="btn-group">
													<button class="btn btn-small" ng-click="editprivs(privs.global)" ng-disabled="user=='root'&&host!='%'">
														<i class="icon-edit"></i> 编辑权限
													</button>
												</div>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
						</div>
					</div>
					
				</div>
			</div>

			<div class="tab-pane" ng-class="'active' | iftrue:activeTabName=='operation'" id="operation">
				<div class="well" style="width:350px" ng-show="privsloading">
					<img src="images/loading.gif" style="margin-right:5px"> 正在加载用户信息，请稍候...
				</div>

				<div class="row-fluid">
					<div class="span6">
						<div class="widget" ng-show="!privsloading">
							<div class="widget-header">
								<i class="icon-lock"></i><h3>密码设置</h3>
							</div>
							<div class="widget-content">
								<div class="form-horizontal form-horizontal-small">
									<div class="control-group control-group-mini">
										<div class="controls">
											<label class="checkbox inline">
												<input type="checkbox" ng-model="emptypassword"> 使用空密码
											</label>
										</div>
									</div>
									<div class="control-group control-group-mini">
										<label class="control-label">新密码</label>
										<div class="controls">
											<input type="password" ng-disabled="processing||emptypassword" ng-model="newpassword" placeholder="不修改请留空">
										</div>
									</div>
									<div class="control-group control-group-mini">
										<label class="control-label">确认密码</label>
										<div class="controls">
											<input type="password" ng-disabled="processing||emptypassword" ng-model="newpasswordc" placeholder="不修改请留空">
										</div>
									</div>
									<div class="control-group control-group-mini">
										<div class="controls">
											<button class="btn" ng-disabled="processing||(!emptypassword&&!newpassword)" ng-click="setpassword()">更新密码</button>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="span6" ng-show="user!='root'||host=='%'">
						<div class="widget" ng-show="!privsloading">
							<div class="widget-header">
								<i class="icon-remove"></i><h3>删除用户</h3>
							</div>
							<div class="widget-content">
								<div class="input-append">
									<input class="input-small" type="text" ng-disabled="processing" ng-model="delete_confirm" placeholder="输入 OK">
									<button class="btn" ng-disabled="processing||delete_confirm!='OK'" ng-click="dropuser()">输入大写 OK 后确认删除</button>
								</div>
							</div>
						</div>
					</div>
				</div>

			</div>
		</div>
	</div>
</div>

<div id="privsedit" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>{{privsedit_title}}</h3>
  </div>
  <div class="modal-body">
	<fieldset>
		<div class="row-fluid">
			<fieldset class="span3">
				<legend>数据权限</legend>
				<label class="checkbox" title="允许读取数据">
					<input type="checkbox" ng-model="curprivs.Select_priv" ng-true-value="Y" ng-false-value="N">
					SELECT
				</label>
				<label class="checkbox" title="允许插入和替换数据">
					<input type="checkbox" ng-model="curprivs.Insert_priv" ng-true-value="Y" ng-false-value="N">
					INSERT
				</label>
				<label class="checkbox" title="允许修改数据">
					<input type="checkbox" ng-model="curprivs.Update_priv" ng-true-value="Y" ng-false-value="N">
					UPDATE
				</label>
				<label class="checkbox" title="允许删除数据">
					<input type="checkbox" ng-model="curprivs.Delete_priv" ng-true-value="Y" ng-false-value="N">
					DELETE
				</label>
				<label class="checkbox" title="允许从文件中导入数据以及将数据导出至文件" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.File_priv" ng-true-value="Y" ng-false-value="N">
					FILE
				</label>
			</fieldset>
			<fieldset class="span5">
				<legend>结构权限</legend>
				<label class="checkbox" title="允许创建新数据库和数据表。">
					<input type="checkbox" ng-model="curprivs.Create_priv" ng-true-value="Y" ng-false-value="N">
					CREATE
				</label>
				<label class="checkbox" title="允许修改现有数据表的结构">
					<input type="checkbox" ng-model="curprivs.Alter_priv" ng-true-value="Y" ng-false-value="N">
					ALTER
				</label>
				<label class="checkbox" title="允许创建和删除索引">
					<input type="checkbox" ng-model="curprivs.Index_priv" ng-true-value="Y" ng-false-value="N">
					INDEX
				</label>
				<label class="checkbox" title="允许删除数据库和数据表">
					<input type="checkbox" ng-model="curprivs.Drop_priv" ng-true-value="Y" ng-false-value="N">
					DROP
				</label>
				<label class="checkbox" title="允许创建临时表">
					<input type="checkbox" ng-model="curprivs.Create_tmp_table_priv" ng-true-value="Y" ng-false-value="N">
					CREATE TEMPORARY TABLES
				</label>
				<label class="checkbox" title="允许执行 SHOW CREATE VIEW 查询">
					<input type="checkbox" ng-model="curprivs.Show_view_priv" ng-true-value="Y" ng-false-value="N">
					SHOW VIEW
				</label>
				<label class="checkbox" title="允许创建存储过程">
					<input type="checkbox" ng-model="curprivs.Create_routine_priv" ng-true-value="Y" ng-false-value="N">
					CREATE ROUTINE
				</label>
				<label class="checkbox" title="允许修改或删除储存过程">
					<input type="checkbox" ng-model="curprivs.Alter_routine_priv" ng-true-value="Y" ng-false-value="N">
					ALTER ROUTINE
				</label>
				<label class="checkbox" title="允许运行存储过程">
					<input type="checkbox" ng-model="curprivs.Execute_priv" ng-true-value="Y" ng-false-value="N">
					EXECUTE
				</label>
				<label class="checkbox" title="允许创建视图">
					<input type="checkbox" ng-model="curprivs.Create_view_priv" ng-true-value="Y" ng-false-value="N">
					CREATE VIEW
				</label>
				<label class="checkbox" title="允许为事件触发器设置事件">
					<input type="checkbox" ng-model="curprivs.Event_priv" ng-true-value="Y" ng-false-value="N">
					EVENT
				</label>
				<label class="checkbox" title="允许创建和删除触发器">
					<input type="checkbox" ng-model="curprivs.Trigger_priv" ng-true-value="Y" ng-false-value="N">
					TRIGGER
				</label>
			</fieldset>
			<fieldset class="span4">
				<legend>管理权限</legend>
				<label class="checkbox" title="允许添加用户和权限，而不允许重新载入权限表">
					<input type="checkbox" ng-model="curprivs.Grant_priv" ng-true-value="Y" ng-false-value="N">
					GRANT
				</label>
				<label class="checkbox" title="允许在达到最大连接数时连接，对于大多数像设置全局变量或杀死其它用户线程这样的管理操作是必需的" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Super_priv" ng-true-value="Y" ng-false-value="N">
					SUPER
				</label>
				<label class="checkbox" title="允许查看所有用户的进程" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Process_priv" ng-true-value="Y" ng-false-value="N">
					PROCESS
				</label>
				<label class="checkbox" title="允许重新载入服务器设置并刷新服务器的缓存" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Reload_priv" ng-true-value="Y" ng-false-value="N">
					RELOAD
				</label>
				<label class="checkbox" title="允许关闭服务器" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Shutdown_priv" ng-true-value="Y" ng-false-value="N">
					SHUTDOWN
				</label>
				<label class="checkbox" title="允许访问完整的数据库列表" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Show_db_priv" ng-true-value="Y" ng-false-value="N">
					SHOW DATABASES
				</label>
				<label class="checkbox" title="允许锁定当前线程的表">
					<input type="checkbox" ng-model="curprivs.Lock_tables_priv" ng-true-value="Y" ng-false-value="N">
					LOCK TABLES
				</label>
				<label class="checkbox" title="允许创建外键约束">
					<input type="checkbox" ng-model="curprivs.References_priv" ng-true-value="Y" ng-false-value="N">
					REFERENCES
				</label>
				<label class="checkbox" title="用户有权询问附属者/控制者在哪里" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Repl_client_priv" ng-true-value="Y" ng-false-value="N">
					REPLICATION CLIENT
				</label>
				<label class="checkbox" title="允许主从模式中的从复制" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Repl_slave_priv" ng-true-value="Y" ng-false-value="N">
					REPLICATION SLAVE
				</label>
				<label class="checkbox" title="允许创建、删除和重命名用户账户" ng-show="!curprivs.Db" ng-disabled="curprivs.Db!=''">
					<input type="checkbox" ng-model="curprivs.Create_user_priv" ng-true-value="Y" ng-false-value="N">
					CREATE USER
				</label>
			</fieldset>
		</div>
	</fieldset>
  </div>
  <div class="modal-footer">
	<div class="pull-left">
		<label class="checkbox">
			<input type="checkbox" ng-model="selectall"> 全选/全不选
		</label>
	</div>
    <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
    <button class="btn btn-primary" onclick="$('#privsedit').modal('hide')" ng-click="updateprivs()">保存权限设置</button>
  </div>
</div>
